package com.hoshiicloud.saleorder.mapper.customer;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hoshiicloud.saleorder.param.customer.ReviewsQueryParam;
import com.hoshiicloud.user.entity.customer.Reviews;
import com.hoshiicloud.user.vo.customer.ReviewsDetailVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 平台商品评价 Mapper 接口
 * </p>
 *
 * @author Mr.Wu
 * @since 2019-04-28
 */
@Repository
public interface ReviewsMapper extends BaseMapper<Reviews> {
	@SelectProvider(type = ReviewsSqlBuilder.class, method = "selectReviewsAndExtraComment")

	public IPage<ReviewsDetailVO> selectReviewsAndExtraCommentPage(IPage<Reviews> page,@Param("param") ReviewsQueryParam param);

	@SelectProvider(type = ReviewsSqlBuilder.class, method = "selectReviewsAndExtraComment")
	public List<ReviewsDetailVO> selectReviewsAndExtraCommentList(@Param("param") ReviewsQueryParam param);

	@SelectProvider(type = ReviewsSqlBuilder.class, method = "selectReviewsAndExtraCommentById")
	public ReviewsDetailVO selectReviewsAndExtraCommentOne(@Param("id") String id);

	class ReviewsSqlBuilder {
		public String selectReviewsAndExtraCommentById(Map map) {
			return new SQL() {
				{
					SELECT("a.*,b.id as extraId,b.contexts as extraContexts,b.createdtime as extraCreatedTime");
					FROM("reviews a");
					LEFT_OUTER_JOIN("reviews b on (a.orderid=b.orderid and b.review_type=2)");
					WHERE("a.id=#{id}");
				}
			}.toString();
		};

		public String selectReviewsAndExtraComment(Map map) {
			return new SQL() {
				{
					ReviewsQueryParam param=(ReviewsQueryParam)map.get("param");
					SELECT("a.*,b.id as extraId,b.contexts as extraContexts,b.createdtime as extraCreatedTime");
					FROM("reviews a");
					LEFT_OUTER_JOIN("reviews b on (a.orderid=b.orderid and b.review_type=2)");
					WHERE("a.review_type=1");
					if (param.getPlatformSpuId() != null) {
						WHERE("a.platform_spu_id=" + param.getPlatformSpuId());
					}
					if (param.getPlatformSkuId() != null) {
						WHERE("a.platform_sku_id=" + param.getPlatformSkuId());
					}
					if (param.getId() != null) {
						WHERE("a.id=#{param.id}");
					}
					if (param.getOrderid() != null) {
						WHERE("a.orderid=" + param.getOrderid());
					}
					if (param.getStars() != null) {
						WHERE("a.stars=" + param.getStars());
					}
					if (param.getIsAnonymous() != null) {
						WHERE("a.is_anonymous=" + param.getIsAnonymous());
					}
					if (param.getPlatformId() != null) {
						WHERE("a.platform_id=" + param.getPlatformId());
					}
					ORDER_BY("createdtime desc");
				}
			}.toString();
		};
	}
}
